﻿using LBSoft.Common.DotNetData;
using LBSoft.Common.DotNetJson;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace LBSoft.Business.NewDAL
{
    public class File_Dal
    {
        mJsonResult json = new mJsonResult();
        Common_Dal systemidao = new Common_Dal();

        /// <summary>
        /// 获取附件
        /// </summary>
        public DataTable GetFileDt(string recordGUID, string recordType = "")
        {
            string sql = string.Format("SELECT * FROM T_AnnexFiles WHERE RecordGUID='{0}' AND IsDelete=0", recordGUID);
            if (!string.IsNullOrEmpty(recordType))
            {
                sql += string.Format(" AND RecordType='{0}'", recordType);
            }
            sql += " ORDER BY Sort";

            DataTable dt = systemidao.GetDataTableBySQL(new StringBuilder(sql));
            return dt;
        }

        /// <summary>
        /// 获取附件Json
        /// </summary>
        public string GetFileJson(string recordGUID, string recordType = "")
        {
            string sql = string.Format("SELECT * FROM T_AnnexFiles WHERE RecordGUID='{0}' AND IsDelete=0", recordGUID);
            if (!string.IsNullOrEmpty(recordType))
            {
                sql += string.Format(" AND RecordType='{0}'", recordType);
            }
            sql += " ORDER BY Sort";

            DataTable dt = systemidao.GetDataTableBySQL(new StringBuilder(sql));

            if (dt != null && dt.Rows.Count > 0)
            {
                json.success = true;
                json.rows = dt;
            }
            return JsonConvert.SerializeObject(json);
        }

        /// <summary>
        /// 添加附件
        /// </summary>
        public void AddFile(string recordGUID, string fileJson, string recordType = "")
        {
            if (!string.IsNullOrEmpty(fileJson))
            {
                DataTable dt = JsonConvert.DeserializeObject<DataTable>(fileJson);
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        Hashtable ht = DataTableHelper.DataRowToHashTable(dr);
                        string GUID = Guid.NewGuid().ToString();
                        ht["GUID"] = GUID;
                        if (!string.IsNullOrEmpty(recordType))
                        {
                            ht["RecordType"] = recordType;
                        }
                        ht["RecordGUID"] = recordGUID;
                        ht["IsDelete"] = 0;
                        ht["CreateDate"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        systemidao.SubmitData("T_AnnexFiles", "GUID", "", ht);
                    }
                }
            }
        }

        /// <summary>
        /// 删除附件
        /// </summary>
        public void DelFile(string recordGUID, string recordType = "", bool isDelRecord = false, bool isDelFile = false)
        {
            string sql = string.Format("SELECT * FROM T_AnnexFiles WHERE RecordGUID='{0}'", recordGUID);
            if (!string.IsNullOrEmpty(recordType))
            {
                sql += string.Format(" AND RecordType='{0}'", recordType);
            }
            DataTable dt = systemidao.GetDataTableBySQL(new StringBuilder(sql));

            string delSql = string.Format("update T_AnnexFiles set IsDelete=1 where RecordGUID='{0}'", recordGUID);
            //是否删除记录
            if (isDelRecord)
            {
                delSql = string.Format("delete from T_AnnexFiles where RecordGUID='{0}'", recordGUID);
            }
            if (!string.IsNullOrEmpty(recordType))
            {
                delSql += string.Format(" AND RecordType='{0}'", recordType);
            }
            int flag = systemidao.ExecuteBySql(new StringBuilder(delSql));
            //是否删除文件
            if (flag > 0 && isDelFile)
            {
                if (dt != null && dt.Rows.Count > 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        string path = dr["AnnexPath"].ToString();
                        try
                        {
                            FileInfo fileInfo = new FileInfo(HttpContext.Current.Server.MapPath(path));
                            fileInfo.Delete();
                        }
                        catch (Exception)
                        {
                            // ignored
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 复制附件
        /// </summary>
        public void CopyFile(string recordGUID, string copyToRecordGUID, string recordType = "", string copyToRecordType = "")
        {
            string sql = string.Format("SELECT * FROM T_AnnexFiles WHERE RecordGUID='{0}' AND IsDelete=0", recordGUID);
            if (!string.IsNullOrEmpty(recordType))
            {
                sql += string.Format(" AND RecordType='{0}'", recordType);
            }
            sql += " ORDER BY Sort";

            DataTable dt = systemidao.GetDataTableBySQL(new StringBuilder(sql));
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    Hashtable ht = DataTableHelper.DataRowToHashTable(dr);
                    string GUID = Guid.NewGuid().ToString();
                    ht["GUID"] = GUID;
                    ht["RecordGUID"] = copyToRecordGUID;
                    if (!string.IsNullOrEmpty(copyToRecordType))
                    {
                        ht["RecordType"] = copyToRecordType;
                    }
                    ht["CreateDate"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    systemidao.SubmitData("T_AnnexFiles", "GUID", "", ht);
                }
            }
        }

    }
}
